# @Version        : 1.0
# @Update Time    : 2024/7/4 22:20
# @File           : role_model.py
# @IDE            : PyCharm
# @Desc           : 文件描述信息
from advanced_alchemy.base import BigIntAuditBase

from .m2m import sys_role_menu, sys_role_dept
from src.core.base.orm.orm import AbstractORMModel
from sqlalchemy.orm import Mapped, mapped_column, relationship
from .menu_model import SysMenu
from .dept_model import SysDept
from sqlalchemy import String, Integer, Index


# class SysRole(AbstractORMModel):
#     __tablename__ = "sys_role"
#     __table_args__ = {"comment": "角色表"}
#     role_name: Mapped[str] = mapped_column(
#         String(30), nullable=False, comment="角色名称"
#     )
#     role_key: Mapped[str] = mapped_column(
#         String(100), nullable=False, comment="角色权限字符串"
#     )
#     # 首页key
#     home_key: Mapped[str] = mapped_column(
#         String(100), default="home", comment="首页key"
#     )
#     role_sort: Mapped[int] = mapped_column(Integer, nullable=False, comment="显示顺序")
#     data_scope: Mapped[int] = mapped_column(
#         Integer,
#         default=0,
#         comment="数据范围（0 仅本人数据权限 1 本部门数据权限 2 本部门及以下数据权限 3 自定义数据权限 4 全部数据权限）",
#     )
#     menu_check_strictly: Mapped[int] = mapped_column(
#         Integer, default=1, comment="菜单树选择项是否关联显示"
#     )
#     dept_check_strictly: Mapped[int] = mapped_column(
#         Integer, default=1, comment="部门树选择项是否关联显示"
#     )
#     status: Mapped[str] = mapped_column(
#         String(1), nullable=False, comment="角色状态（1正常 2停用）"
#     )
#     remark: Mapped[str | None] = mapped_column(String(500), comment="备注")
#     # 菜单关系
#     menus: Mapped[set[SysMenu]] = relationship(secondary=sys_role_menu)
#     # 部门关系
#     depts: Mapped[set[SysDept]] = relationship(secondary=sys_role_dept)


class SysRole(BigIntAuditBase):
    __tablename__ = "sys_role"
    __table_args__ = (
        Index("ix_sys_role_role_key", "role_key"),
        Index("ix_sys_role_status", "status"),
        {"comment": "角色表"},
    )
    role_name: Mapped[str] = mapped_column(
        String(30), nullable=False, comment="角色名称"
    )
    role_key: Mapped[str] = mapped_column(
        String(100), nullable=False, comment="角色权限字符串"
    )
    # 首页key
    home_key: Mapped[str] = mapped_column(
        String(100), default="home", comment="首页key"
    )
    role_sort: Mapped[int] = mapped_column(Integer, nullable=False, comment="显示顺序")
    data_scope: Mapped[int] = mapped_column(
        Integer,
        default=0,
        comment="数据范围（0 仅本人数据权限 1 本部门数据权限 2 本部门及以下数据权限 3 自定义数据权限 4 全部数据权限）",
    )
    menu_check_strictly: Mapped[int] = mapped_column(
        Integer, default=1, comment="菜单树选择项是否关联显示"
    )
    dept_check_strictly: Mapped[int] = mapped_column(
        Integer, default=1, comment="部门树选择项是否关联显示"
    )
    status: Mapped[str] = mapped_column(
        String(1), nullable=False, comment="角色状态（1正常 2停用）"
    )
    remark: Mapped[str | None] = mapped_column(String(500), comment="备注")
    # 菜单关系
    menus: Mapped[set["SysMenu"]] = relationship(secondary=sys_role_menu)
    # 部门关系
    depts: Mapped[set["SysDept"]] = relationship(secondary=sys_role_dept)

